Passed
Push — master ( 6f8171...7c2bcd )
by
unknown
48s
created

DataAPI.getDepartments   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 1
rs 10
1
/*
2
 *
3
 * The DataAPI manages the AJAX calls and callbacks to external data sources.
4
 *
5
 * Author:		Gregg Bowden - Deloitte ([email protected])
6
 * Owner:			Deloitte
7
 */
8
9
/**
10
 *
11
 * @returns {undefined}
12
 */
13
var DataAPI = {};
14
var xhr;
0 ignored issues
show
Unused Code introduced by
The variable xhr seems to be never used. Consider removing it.
Loading history...
15
16
DataAPI.version = "v1";
17
//Live URL
18
//DataAPI.baseURL = "http://localhost:8080/contacts/api/"+DataAPI.version+"";
19
//Dev URL
20
DataAPI.baseURL = "/tc/api/"+DataAPI.version+"";
21
//Live REST API URL
22
DataAPI.mockURL = "https://localhost:8083/talentcloud/api/"+DataAPI.version+"";
23
24
/**
25
 *
26
 * @param {String} url - the url endpoint of the request
27
 * @param {String} restMethod - 'GET', 'PUT', 'POST', or 'DELETE'
28
 * @param {String:String map} headersMap - Map of extra reuqest headers for the
29
 *      request. By default, Content-type and Accept are set to 'application/json',
30
 *      though this can be overridden with headersMap.
31
 * @param {Object} payload - the payload of the request
32
 * @param {function} requestCallback - this function will be called upon the'load'
33
 *      event, with the XMLHttpRequest as the single argument
34
 * @param {boolean} isSecondAttempt - this will be true if a request is being 
35
 *      retried for some reason. Otherwise, should be left false or undefined.
36
 * @return {undefined}
37
 */
38
DataAPI.sendRequest = function(url, restMethod, headersMap, payload, requestCallback, isSecondAttempt) {
39
    var request = new XMLHttpRequest();
40
    if ("withCredentials" in request) {
41
        // Check if the XMLHttpRequest object has a "withCredentials" property.
42
        // "withCredentials" only exists on XMLHTTPRequest2 objects.
43
        request.open(restMethod, url);
44
45
    } else if (typeof XDomainRequest != "undefined") {
0 ignored issues
show
Bug introduced by
The variable XDomainRequest seems to be never declared. If this is a global, consider adding a /** global: XDomainRequest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
        // Otherwise, check if XDomainRequest.
47
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
48
        request = new XDomainRequest();
49
        request.open(restMethod, url);
50
    } else {
51
        // Otherwise, CORS is not supported by the browser.
52
        request = null;
53
        // TODO: indicate to user that browser is not supported
54
    }
55
    //Default to application/json if content-type not included in headersMap
56
    if (!headersMap['Content-type'] && !headersMap['Content-Type'])
57
        request.setRequestHeader("Content-type", "application/json");
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
58
    if (!headersMap['Accept'])
0 ignored issues
show
Coding Style introduced by
['Accept'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
59
        request.setRequestHeader("Accept", "application/json");
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
60
    //Set custom headers
61
    var keys = Object.keys(headersMap)
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
62
    for (var i=0; i<keys.length; i++) {
63
        request.setRequestHeader(keys[i], headersMap[keys[i]]);
64
    }
65
66
    request.addEventListener("progress", DataAPI.updateProgress, false);
67
    request.addEventListener("error", DataAPI.transferFailed, false);
68
    request.addEventListener("abort", DataAPI.transferAborted, false);
69
    request.addEventListener("load", function() {
70
        //If there was an authentication error, retry, but only once (to avoid
71
        //  infinite retries)
72
        if (!isSecondAttempt && request.status === 401) {
73
            DataAPI.sendRequest(url, restMethod, headersMap, payload, requestCallback, true);            
74
        } else {
75
           requestCallback(request); 
76
        }
77
    },false);
78
79
    request.send(payload);
80
};
81
82
DataAPI.getStaticContent = function(locale, requestCallback){
83
    var talentcloudData_URL = DataAPI.baseURL+"/"+locale+"/getContent";
84
    DataAPI.sendRequest(talentcloudData_URL, "GET", {'Content-type': 'application/json; charset=utf-8'}, null, requestCallback);
85
};
86
87
/**
88
 *
89
 * @param {type} evt
90
 * @returns {undefined}
91
 */
92
DataAPI.talentcloudDataUpdateProgress = function(evt){
93
    Utilities.debug?console.log(evt):null;
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
94
    if(evt.lengthComputable){
95
        var total = evt.total;
96
        var value = evt.loaded;
97
98
        Utilities.debug?console.log(total + "=" + value):null;
0 ignored issues
show
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
99
100
        var percentComplete = Math.ceil((evt.loaded / evt.total) * 100);
0 ignored issues
show
Unused Code introduced by
The variable percentComplete seems to be never used. Consider removing it.
Loading history...
101
        //var loadingProgress = document.getElementById("loadingProgress");
102
        //loadingProgress.innerHTML = " " + percentComplete + "%";
103
    }
104
};
105
106
/**
107
 *
108
 * @param {type} locale
109
 * @returns {undefined}
110
 */
111
DataAPI.getJobs = function(responseCallback){
112
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
113
    var jobs_url = DataAPI.baseURL+"/"+locale+"/getAllJobs";
114
    DataAPI.sendRequest(jobs_url, 'GET', {}, null, responseCallback);
115
};
116
117
DataAPI.getDepartments = function(locale){
118
    var departments_url = DataAPI.baseURL+"/"+locale+"/Lookup/department";
119
    DataAPI.sendRequest(departments_url, 'GET', {}, null, function(request){
120
        DataAPI.loadedManagerDepartments(request.response);
121
    });
122
};
123
124
/**
125
 *
126
 * @param {int} user_id
127
 * @param {function} successfulResponseCallback - this will be called if the
128
 *  request comes back with readyState==4 and status==200
129
 * @return {undefined}
130
 */
131
DataAPI.getJobSeekerProfileByUserId = function(user_id, successfulResponseCallback){
132
    console.log("getJobSeekerProfileByUserId");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
133
    Utilities.debug?console.log("loading job seekers"):null;
0 ignored issues
show
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
134
    var jobSeekers_url = DataAPI.baseURL+"/getJobSeekerProfileByUser/"+user_id;
135
    DataAPI.sendRequest(jobSeekers_url, "GET", {}, null, function(request) {
136
        if(request.readyState === 4 && request.status === 200){
137
            successfulResponseCallback(request.response);
138
        }
139
    });
140
};
141
142
/**
143
 *
144
 * @param {type} evt
145
 * @returns {undefined}
146
 */
147
DataAPI.updateProgress = function(evt){
148
    Utilities.debug?console.log(evt):null;
0 ignored issues
show
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
149
    if(evt.lengthComputable){
150
        var total = evt.total;
151
        var value = evt.loaded;
152
153
        Utilities.debug?console.log(total + "=" + value):null;
0 ignored issues
show
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
154
155
        var percentComplete = Math.ceil((evt.loaded / evt.total) * 100);
0 ignored issues
show
Unused Code introduced by
The variable percentComplete seems to be never used. Consider removing it.
Loading history...
156
        //var loadingProgress = document.getElementById("loadingProgress");
157
        //loadingProgress.innerHTML = " " + percentComplete + "%";
158
    }
159
};
160
161
DataAPI.loadedManagerDepartments = function(response){
162
    setTimeout(function(){
163
        DepartmentAPI.loadFromJSON(JSON.parse(response));
0 ignored issues
show
Bug introduced by
The variable DepartmentAPI seems to be never declared. If this is a global, consider adding a /** global: DepartmentAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
164
    }
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before ,.
Loading history...
165
    ,1000);
0 ignored issues
show
introduced by
Comma warnings can be turned off with 'laxcomma'.
Loading history...
166
};
167
168
/**
169
 *
170
 * @returns {undefined}
171
 */
172
DataAPI.transferFailed = function(){
173
    NetworkErrorMessage();
174
};
175
176
DataAPI.getJobPoster = function(locale, jobId, responseCallback){
177
    Utilities.debug?console.log("loading job seekers"):null;
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
178
    var jobPoster_url = DataAPI.baseURL+"/"+locale+"/getJobPoster/"+jobId;
179
    DataAPI.sendRequest(jobPoster_url, 'GET', {}, null, function(request) {
180
        responseCallback(request.response);
181
    });
182
};
183
184
DataAPI.getAllJobsByAdminUserId = function(locale, userId, requestCallback) {
185
    var url = [DataAPI.baseURL, locale, "getAllJobsByAdminUser", userId].join('/');
186
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
187
};
188
189
DataAPI.getAllJobApplicationsByJobPosterId = function(jobPosterId, requestCallback) {
190
    var url = [DataAPI.baseURL, 'getJobApplicationsByJobPoster', jobPosterId].join('/');
191
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
192
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
193
194
DataAPI.getManagerProfile = function(userId, responseCallback) {
195
    var manager_profile_url = DataAPI.baseURL + "/getManagerProfile/"+userId;
196
    DataAPI.sendRequest(manager_profile_url, "GET", {}, null, function(request) {
197
        responseCallback(request.response);
198
    });
199
};
200
201
/**
202
 *
203
 * @param {JobApplicationAPI.JobApplication} jobApplication
204
 * @param {function} responseCallback
205
 * @return {undefined}
206
 */
207
DataAPI.createJobApplication = function(jobApplication, requestCallback) {
208
    var url = DataAPI.baseURL + '/postJobApplication';
209
    DataAPI.sendRequest(url, "POST", {}, JSON.stringify(jobApplication), requestCallback);
210
};
211
212
213
DataAPI.saveJobApplicationByJobAndUser = function(jobApplication, jobPosterId, userId, requestCallback) {
214
    var url = [DataAPI.baseURL, "putApplicationForJob", jobPosterId, "forUser", userId].join("/");
215
    DataAPI.sendRequest(url, "PUT", {}, JSON.stringify(jobApplication), requestCallback);
216
};
217
218
DataAPI.getJobApplicationByJobAndUser = function(jobPosterId, userId, requestCallback) {
219
    var url = [DataAPI.baseURL, "getApplicationForJob", jobPosterId, "forUser", userId].join("/");
220
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
221
};
222
223
DataAPI.submitJobApplication = function(applicationId, requestCallback) {
224
    var url = [DataAPI.baseURL, "submitJobApplication", applicationId].join("/");
225
    DataAPI.sendRequest(url, "POST", {}, null, requestCallback);
226
};
227
228
DataAPI.getFullJobApplicationByJobAndUser = function(jobPosterId, userId, requestCallback) {
229
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
230
    var url = [DataAPI.baseURL, locale, "getFullApplicationForJob", jobPosterId, "forUser", userId].join("/");
231
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
232
};
233
234
DataAPI.getFullJobApplication = function(jobPosterApplicationId, requestCallback) {
235
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
236
    var url = [DataAPI.baseURL, locale, "getFullApplication", jobPosterApplicationId].join("/");
237
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
238
};
239
240
/**
241
 *
242
 * @param {int} managerProfileId
243
 * @param {CreateWorkEnvironment.WorkEnvironment} workplaceEnvironment
244
 * @param {function} responseCallback
245
 * @return {undefined}
246
 */
247
DataAPI.submitWorkplaceEnvironment = function(managerProfileId, workplaceEnvironment, responseCallback) {
248
    var url = DataAPI.baseURL + '/putWorkEnvironmentByManagerProfile/' + managerProfileId;
249
    DataAPI.sendRequest(url, "PUT", {}, JSON.stringify(workplaceEnvironment), function(request) {
250
        responseCallback(request.response);
251
    });
252
};
253
254
/**
255
 *
256
 * @param {int} managerProfileId
257
 * @param {function} responseCallback
258
 * @return {undefined}
259
 */
260
DataAPI.getWorkplaceEnvironment = function(managerProfileId, responseCallback) {
261
    var url = DataAPI.baseURL + '/getWorkEnvironmentByManagerProfile/' + managerProfileId;
262
    DataAPI.sendRequest(url, 'GET', {}, null, function(request) {
263
        responseCallback(request.response);
264
    });
265
};
266
267
DataAPI.getSkillDeclarationsForApplication = function(applicationId, requestCallback) {
268
    var url = [DataAPI.baseURL, "getDeclarationsForApplication", applicationId].join("/");
269
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
270
};
271
272
DataAPI.saveSkillDeclaration = function(skillDeclaration, criteriaId, applicationId, requestCallback) {
273
    var url = DataAPI.baseURL + "/putDeclarationForApplication/" + applicationId + "/forCriteria/" + criteriaId;
274
    DataAPI.sendRequest(url, 'PUT', {}, JSON.stringify(skillDeclaration), requestCallback);
275
};
276
277
DataAPI.deleteSkillDeclaration = function(criteriaId, applicationId, requestCallback) {
278
    var url = DataAPI.baseURL + "/deleteDeclarationForApplication/" + applicationId + "/forCriteria/" + criteriaId;
279
    DataAPI.sendRequest(url, 'DELETE', {}, null, requestCallback);
280
};
281
282
DataAPI.getMicroReferencesForApplication = function(applicationId, requestCallback) {
283
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
284
    var url = [DataAPI.baseURL, locale, "getAllMicroReferencesForApplication", applicationId].join("/");
285
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
286
};
287
288
DataAPI.saveMicroReference = function(microReference, criteriaId, applicationId, requestCallback) {
289
    var url = DataAPI.baseURL + "/putMicroReferenceForApplication/" + applicationId + "/forCriteria/" + criteriaId;
290
    DataAPI.sendRequest(url, 'PUT', {}, JSON.stringify(microReference), requestCallback);
291
};
292
293
DataAPI.deleteMicroReference = function(criteriaId, applicationId, requestCallback) {
294
    var url = DataAPI.baseURL + "/deleteMicroReferenceForApplication/" + applicationId + "/forCriteria/" + criteriaId;
295
    DataAPI.sendRequest(url, 'DELETE', {}, null, requestCallback);
296
};
297
298
DataAPI.getSkillSamplesForApplication = function(applicationId, requestCallback) {
299
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
300
    var url = [DataAPI.baseURL, locale, "getAllWorkSamplesForApplication", applicationId].join("/");
301
    DataAPI.sendRequest(url, "GET", {}, null, requestCallback);
302
};
303
304
DataAPI.saveSkillSample = function(skillSample, criteriaId, applicationId, requestCallback) {
305
    var url = DataAPI.baseURL + "/putWorkSampleForApplication/" + applicationId + "/forCriteria/" + criteriaId;
306
    DataAPI.sendRequest(url, 'PUT', {}, JSON.stringify(skillSample), requestCallback);
307
};
308
309
DataAPI.deleteSkillSample = function(criteriaId, applicationId, requestCallback) {
310
    var url = DataAPI.baseURL + "/deleteWorkSampleForApplication/" + applicationId + "/forCriteria/" + criteriaId;
311
    DataAPI.sendRequest(url, 'DELETE', {}, null, requestCallback);
312
};
313